$(function() {
	/*外页上传*/
	var $list = $('#fileList_pre_picture'),

	ratio = window.devicePixelRatio || 1,
	thumbnailWidth = 100 * ratio,
	thumbnailHeight = 100 * ratio,
	uploader,uploader_pre_prcture_inn;
	var web_path = document.getElementById("web_path").value;
	uploader = WebUploader.create({
		auto: true,
		swf: 'lib/webuploader/0.1.5/Uploader.swf',
		server: web_path+'/backstage/common/uploadPic',
		pick: '#filePicker_pre_picture',
		
		formData : {
			foldername : 'template'
		}
	});

	uploader.on( 'fileQueued', function( file ) {
		var $li = $(
			'<div id="' + file.id + '" class="file-item thumbnail">' +
			'<img>' +
			'<div class="info">' + file.name + '</div>' +
			'</div>'
			),
		$img = $li.find('img');

		$list.append( $li );
		uploader.makeThumb( file, function( error, src ) {
			if ( error ) {
				$img.replaceWith('<span>不能预览</span>');
				return;
			}

			$('#pre_img').attr( 'src', src );
			$img.attr( 'src', src );
		}, thumbnailWidth, thumbnailHeight );
	});

	uploader.on( 'uploadSuccess', function(file, response) {
		$( '#'+file.id ).addClass('upload-state-done');
		if( response.datas == ''){
			$error.text('上传失败');
		}else{			
			$("#tpl_thumb").val(response.datas);
		}
	});

	uploader.on( 'uploadError', function( file ) {
		var $li = $( '#'+file.id ),
		$error = $li.find('div.error');
		if ( !$error.length ) {
			$error = $('<div class="error"></div>').appendTo( $li );
		}

		$error.text('上传失败');
	});

	uploader.on( 'uploadComplete', function( file ) {
		$( '#'+file.id ).find('.progress').remove();
	});
});
